/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.service;

import com.yunqian.redot.domain.SwiftpassPay;
import com.yunqian.redot.repository.SwiftpassPayRepository;
import com.yunqian.service.BaseService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author czy
 * 2018-10-17 15:03:49
 */
@Log4j2
@Service
public class SwiftpassPayService extends BaseService<SwiftpassPay, Long> {

    @Autowired
    private SwiftpassPayRepository swiftpassPayRepository;

    public SwiftpassPay getByOutTradeNo(String outTradeNo){
        return swiftpassPayRepository.findByOutTradeNo(outTradeNo);
    }

    public SwiftpassPay save(String outTradeNo, Map<String,String> params) {
        SwiftpassPay sp = new SwiftpassPay();
        sp.setTimeStamp(params.get("timeStamp"));
        sp.setPrepayId(params.get("package"));
        sp.setPaySign(params.get("paySign"));
        sp.setNonceStr(params.get("nonceStr"));
        sp.setOutTradeNo(outTradeNo);
        return super.save(sp);
    }

    public void update(String outTradeNo, String payResult, String payInfo, String transactionId,
                       String outTransactionId, String totalFee, String bankType, String timeEnd) {
        SwiftpassPay sp = this.getByOutTradeNo(outTradeNo);
        sp.setPayResult(Integer.valueOf(payResult));
        sp.setPayInfo(payInfo);
        sp.setTransactionId(transactionId);
        sp.setOutTransactionId(outTransactionId);
        sp.setTotalFee(Integer.valueOf(totalFee));
        sp.setBankType(bankType);
        sp.setTimeEnd(timeEnd);
        super.update(sp);
    }
}
